#ifndef ROBOT_STATUS_SERVICE_H
#define ROBOT_STATUS_SERVICE_H

#include "rclcpp/rclcpp.hpp"
#include <QCoreApplication>
#include <QObject>
#include <QTimer>
#include <memory>
#include <QMutex>
#include <QDateTime>
#include "../include/seer/SCStatusTcp.h"
#include "../include/seer/RequestManager.h"
#include "../include/global_logger.h"
#include "rms_pkg/srv/robot_status.hpp"

class RobotStatusService : public QObject, public rclcpp::Node
{
    Q_OBJECT

public:
    RobotStatusService();

private:
    void handle_service_request(
        const std::shared_ptr<rms_pkg::srv::RobotStatus::Request> request,
        const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    
    bool isConnected() const;
    bool getBatteryInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    bool getPositionInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    bool getNavigationStateInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    bool getControlAuthorityInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);  // 获取控制权信息
    bool getNavigationAndControlLocationInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response); // 获取导航状态和控制权所有者和位置信息
    bool getAllRobotStatus(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    bool handleOperationCompleted(int cmdId, const QByteArray& resp, std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    bool sendRequestAndHandleResponse(int cmdId, std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    QString getStateString(int taskStatus);
    
    // 新增方法：获取当前地图和下载地图
    bool getCurrentMapInfo(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    void downloadMap(const QString& mapName);

    // 缓存相关
    bool isCacheValid() const;
    void updateCache(const std::shared_ptr<rms_pkg::srv::RobotStatus::Response> response);
    
    // 连接管理
    void initializeConnections();
    void connectToRobot();
    
    // 改进的重连机制
    void scheduleReconnect();
    void resetReconnectAttempts();
    
    // 健康检查
    void healthCheck();
    void updateConnectionStatus();
    
    rclcpp::Service<rms_pkg::srv::RobotStatus>::SharedPtr service_;
    SCStatusTcp* m_statSocket;
    RequestManager* m_requestManager;
    
    // 重连定时器
    QTimer* m_reconnectTimer;
    QTimer* m_healthCheckTimer;  // 健康检查定时器
    
    // 连接参数
    QString m_robotIp;
    int m_statPort;
    
    // 连接状态改进
    bool m_wasConnected;  // 记录上一次连接状态
    int m_reconnectAttempts;  // 重连尝试次数
    // 最大重连次数
    const int MAX_RECONNECT_ATTEMPTS = 10;  // 最大重连次数

    mutable QMutex m_dataMutex;             // 互斥锁
    
    // Robot状态缓存机制
    struct CachedStatus {
        rms_pkg::srv::RobotStatus::Response status;
        QDateTime timestamp;
        int cacheTimeoutMs = 1000; // 1秒缓存
    };
    
    CachedStatus cachedStatus_;
    QString currentMap = "default";

private slots:
    void reconnectTimerTimeout();
    void handleSocketStateChanged(QAbstractSocket::SocketState state);
    void handleSocketError(QAbstractSocket::SocketError error);
    void healthCheckTimeout();  // 健康检查槽函数
};

#endif // ROBOT_STATUS_SERVICE_H